package com.serv.config.druid;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;
@Configuration  
@EnableTransactionManagement 
/**
 * Druid的DataResource配置类
 * @author Raye
 * @since 2016年10月7日14:14:18
 */
public class DruidDataSourceConfig  implements EnvironmentAware {

    private RelaxedPropertyResolver pr_ds_boot;
    private RelaxedPropertyResolver pr_druid;

    public void setEnvironment(Environment env) {
        this.pr_ds_boot = new RelaxedPropertyResolver(env, "spring.datasource_boot.");
        this.pr_druid = new RelaxedPropertyResolver(env, "spring.datasource_boot.druid.");
    }
    
    //@Bean
    public DataSource dataSource() {
        System.out.println("注入druid！！！");
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(pr_ds_boot.getProperty("url"));
        datasource.setUsername(pr_ds_boot.getProperty("username"));
        datasource.setPassword(pr_ds_boot.getProperty("password"));
        datasource.setDriverClassName(pr_ds_boot.getProperty("driverClassName"));

        datasource.setInitialSize(Integer.valueOf(pr_druid.getProperty("initialSize")));
        datasource.setMinIdle(Integer.valueOf(pr_druid.getProperty("minIdle")));
        datasource.setMaxWait(Long.valueOf(pr_druid.getProperty("maxWait")));
        datasource.setMaxActive(Integer.valueOf(pr_druid.getProperty("maxActive")));
        datasource.setMinEvictableIdleTimeMillis(Long.valueOf(pr_druid.getProperty("minEvictableIdleTimeMillis")));
        try {
			datasource.setFilters("stat,wall");
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return datasource;
    }
    
    //
    //
//  @Bean
//  public DataSource dataSource() {
//      DruidDataSource dataSource = new DruidDataSource();
//      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//      dataSource.setUrl("jdbc:mysql://192.168.157.131:3306/springboot");
//      dataSource.setUsername("code");
//      dataSource.setMaxActive(30);
//      dataSource.setPassword("123456");
//      DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl("jdbc:mysql://192.168.157.131:3306/springboot");
//      String validationQuery = databaseDriver.getValidationQuery();
//      try {
//          dataSource.setFilters("stat, wall");
//          if (validationQuery != null) {
//              dataSource.setTestOnBorrow(true);
//              dataSource.setValidationQuery(validationQuery);
//          }
//      } catch (Exception e) {
//          e.printStackTrace();
//      }
//      return dataSource;
//  }
//
//  @Bean
//  public SqlSessionFactory sqlSessionFactory() throws Exception {
//      SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//      bean.setDataSource(dataSource());
//      return bean.getObject();
//
//  }

}
